public class MyAtoi {

    public static void main(String[] args) {
        MyAtoi myAtoi = new MyAtoi();
        System.out.println(myAtoi.myAtoi("-2147483648"));
    }

    public int myAtoi(String s) {
        int n = s.length();
        char[] chars = s.toCharArray();
        int index = 0;
        while (true) {
            if (index >= n) return 0;
            if (chars[index] != 32) break;
            index++;
        }
        if (!(chars[index] >= 48 || chars[index] <= 57
                || chars[index] == '-' || chars[index] == '+')) return 0;
        boolean flag = false;
        if (chars[index] == '-') {
            flag = true;
            index++;
        } else if (chars[index] == '+') index++;
        StringBuffer stringBuffer = new StringBuffer();
        while (!(index == n || chars[index] < 48 || chars[index] > 57)) {
            stringBuffer.append(chars[index++]);
        }
        int temp = 0;
        char[] chars1 = stringBuffer.toString().toCharArray();
        for (int i = 0; i < chars1.length; i++) {
            int k = Integer.parseInt(chars1[i] + "");
            if (Integer.MAX_VALUE / 10 < temp || Integer.MAX_VALUE - temp * 10 < k) return flag ? Integer.MIN_VALUE : Integer.MAX_VALUE;
            temp = (temp * 10 + Integer.parseInt(chars1[i] + ""));
        }
        return flag ? -temp : temp;
    }
}
